package com.shiyu.system.controller;

import com.shiyu.common.core.BaseController.BaseController;
import com.shiyu.common.core.domain.R;
import com.shiyu.common.security.exception.ServiceException;
import com.shiyu.system.domain.LoginDTO;
import com.shiyu.system.domain.SysUserSaveDTO;
import com.shiyu.system.service.ISysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.ClientHttpResponseStatusCodeException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/sysUser")
@Tag(name="管理员接口")
public class SysUserController extends BaseController {
//    登陆成功失败/失败的原因
//    message String  int code   1成功 0失败
//    请求方法和url  sysuser/login
//    接口文档 定义一个统一的响应数据的结构
//    接口的地址/sysUser/login
//    swagger生成文档
    @Autowired
    private ISysUserService iSysUserService;
    @PostMapping("/login")//安全性,请求方法post  请求参Voiddy
    @Operation(summary = "管理员登录",description = "根据账号和密码进行管理员登录")
    @ApiResponse(responseCode = "1000",description = "操作成功")
    @ApiResponse(responseCode = "2000",description = "服务器繁忙,稍后重试")
    @ApiResponse(responseCode = "3102",description = "用户不存在")
    @ApiResponse(responseCode = "3103",description = "用户名或密码错误")
    public R<String>  login(@RequestBody LoginDTO loginDTO){
        System.out.println("system");
     return iSysUserService.login(loginDTO.getUserAccount(), loginDTO.getPassword());
    }
    @PostMapping("/add")
    @Operation(summary = "新增管理员",description = "根据提示的信息新增远管理员")
    @ApiResponse(responseCode = "1000",description = "操作成功")
    @ApiResponse(responseCode = "2000",description = "服务器繁忙,稍后重试")
    @ApiResponse(responseCode = "3000",description = "操作失败")
    public R<Void> add(@RequestBody SysUserSaveDTO sysUserSaveDTO){

//        对于参数的合法性判断
//        checkParams(sysUserSaveDTO);
        return  toR(iSysUserService.add(sysUserSaveDTO));
    }

//  public  void checkParams(SysUserSaveDTO sysUserSaveDTO){
//        String password=sysUserSaveDTO.getPassword();
//        if(password==null){
//            throw new ServiceException();
//        }
//  }

}
